<?php

require_once WACT_ROOT . 'controller/controller.inc.php';

require_once WACT_ROOT . 'controller/form.inc.php';
//require_once WACT_ROOT . 'util/dataspace.inc.php';
require_once APP_ROOT . "view/common/layout.view.php";
require_once APP_ROOT . "control/pub/pub.index.php";
require_once APP_ROOT . "model/common/user.php";
require_once APP_ROOT . "model/common/subscription.php";
require_once APP_ROOT . "model/common/session.php";


class LoginView extends LayoutView {

    function LoginView($TemplateFile) {
        parent :: View($TemplateFile);
    }



    function prepare() {
    	parent::prepare();
		$this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
		$this->Template->set('title',Site::getTitle().' - Login');

     	if(APP_NAME!=""){
			$this->Template->set('app_name',APP_NAME);
		} else $this->Template->set('app_name','.');

    }
}

class NotLoginView extends LayoutView {

    function NotLoginView($TemplateFile) {
        parent :: View($TemplateFile);
    }

    function prepare(&$source, &$request, &$responseModel) {
    	parent::prepare();

		$this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
		$this->Template->set('title',Site::getTitle().' - Login');

    	if(APP_NAME!=""){
			$this->Template->set('app_name',APP_NAME);
		} else $this->Template->set('app_name','.');

     	$this->Template->set('title','Login');
     	if($responseModel->get('login_action')=='action')
     		$this->Template->set('action',' ');
     	else $this->Template->set('unactivated',' ');
    }
}


class LoginPage extends PageController {

    function LoginPage() {
        parent::PageController();
        //print 'LoginPage()';


        $mytemplate = 'pub/login.html';


        $Form = new FormController();
		$this->registerOnLoadListener(new Delegate($this, 'act'));
        $this->addChild('login_form', $Form);

        $this->addView('noaction',new LoginView($mytemplate));
        $this->addView('action',new NotLoginView($mytemplate));
    }

    function act(&$source, &$request, &$responseModel) {
    	$login_action=$request->getPostProperty('login_action');
        //print 'act:login_action'. $login_action;
        //print_r($_POST);
        //print_r($_GET);

    	if($login_action=="action"){
    		$login=$request->getPostProperty('username');
    		$password=$request->getPostProperty('password');
    		//print 'act:login'. $login;
    		//print 'act:password'. $password;
    		$user =& User::getRecordbyLogin($login);
    		if ($user) $id = $user->get('user_id');
    		else  $id = 0;

    		if( $user!=null && $password==$user->get('password') ) {
    			//if($user->get('level')==0)
    				//if(User::isUserSubscriber($id)&&!User::isUserSubscriptionActive($id)){
    				//	$responseModel->set('login_action','unactivated');
    				//	return 'action';
    				//}
    	 		Session::setUser($user);

    	 		$redirect_url='http://'.$_SERVER['HTTP_HOST'].'/index.htm';
    	 		if(defined('SID')&&(!isset($_COOKIE[session_name()])))
    	 		$redirect_url.='?'.SID;
    	 		header('Location: '.$redirect_url);
    	 	}else{
    	 		$responseModel->set('login_action','action');
    			return 'action';
    		}
    	}else return 'noaction';

    }

}

class LogoutPage extends PageController {

    function LogoutPage() {
        parent::PageController();

        Session::removeUser();
        $redirect_url='http://'.$_SERVER['HTTP_HOST'].'/index.htm';
    	if(defined('SID')&&(!isset($_COOKIE[session_name()])))
    			$redirect_url.='?'.SID;
    	header('Location: '.$redirect_url);
    }

}

?>
